package com.dongdongsongcan.app.activity;

import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import com.dongdongsongcan.app.DDApp;
import com.dongdongsongcan.app.R;
import com.dongdongsongcan.app.adapter.ShoplistAdapter;
import com.dongdongsongcan.app.control.ShopControl;
import com.dongdongsongcan.app.control.bean.LabelData;
import com.dongdongsongcan.app.control.bean.ShopData;
import com.dongdongsongcan.app.lib.pulltorefresh.PullToRefreshBase;
import com.dongdongsongcan.app.lib.pulltorefresh.PullToRefreshListView;
import com.su.dolphin.control.task.CtrlTask;
import com.su.dolphin.utils.mgr.DpActivityManager;
import com.su.dolphin.utils.ui.TipsUtil;
import com.su.dolphin.view.activity.BaseActivity;

public class ShoplistActivity extends BaseActivity {
    PullToRefreshListView mShoplist;
    ShoplistAdapter mShoplistAdapter;
    private int mOffset;
    private int PAGE_SIZE = 10;
    private double mSite_x, mSite_y;
    private LabelData.Label mLabel;

    public static void startFrom(Context context, LabelData.Label label) {
        Bundle bundle = new Bundle();
        bundle.putSerializable("label", label);
        DpActivityManager.i().startActivity(context, ShoplistActivity.class, bundle);
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mLabel = (LabelData.Label) this.getIntent().getExtras().getSerializable("label");
        setContentView(R.layout.activity_shoplist);
        setTitle(mLabel.getName() + "商铺列表");
    }

    @Override
    public void initView() {
        mShoplist = (PullToRefreshListView) findViewById(R.id.shop_list);
        mShoplistAdapter = new ShoplistAdapter(this);
        mShoplist.setAdapter(mShoplistAdapter);
    }

    @Override
    public void setListener() {
        mShoplist.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
                mOffset = 0;
                mShoplistAdapter.clear();
                getShops();
            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
                getShops();
            }
        });
        mShoplist.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                ShopDetailActivity.startFrom(ShoplistActivity.this, mShoplistAdapter.getData(position - 1));
                finish();
            }
        });
    }

    @Override
    public void initData() {
        mSite_x = DDApp.getDDLocation().getSite_x();
        mSite_y = DDApp.getDDLocation().getSite_y();
        getShops();
    }

    private void getShops() {
        ShopControl.i().getShops(mLabel.getName(), null, null, mOffset, PAGE_SIZE, mSite_x, mSite_y,
                new CtrlTask.IControlResponse<ShopData>() {

                    @Override
                    public void onControlResponse(ShopData data) {
                        mOffset = data.getOffset();
                        if (mOffset <= data.getNum()) {
                            mShoplistAdapter.addData(data.getResult());
                        } else {
                            TipsUtil.toast("已经是最后一页");
                        }
                        mShoplist.onRefreshComplete();

                    }
                });
    }

}
